package be.technofutur.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LottoServlet2
 */
public class LottoServlet2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private int compte ;
	private int k ;
	private int j =1;
	private List<Integer> choosedNumberArraySession = new ArrayList<Integer>();
	private List<Integer> winningNumberArraySession = new ArrayList<Integer>();
    public void init (ServletConfig Config)
    {
    	compte = 0;
    	k = -1;
    }
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost (request,response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		PrintWriter out = response.getWriter();
		HashMap<String, Integer> cache = new HashMap<String, Integer>();
		SimpleDateFormat timeFormat = new SimpleDateFormat("hh : mm : ss");
		LottoInit lotto = new LottoInit();
		int[] lottoArray = new int[6];
		String [] choosedNumber = {""} ;
		List<Integer> choosedNumberArray = new ArrayList<Integer>();
		List<Integer> winningNumberArray = new ArrayList<Integer>();
		int cpt = 0;
		int c = 0;
        HtmlInit.headerInit(out, "Lotto Game");
        out.println("<h1 align=center>LOTTO</h1>");
        out.print("<center>");
        HttpSession session = request.getSession();
        if(request.getParameter("username")!=null)
        {
        	String username = request.getParameter("username");
        	session.setAttribute("username", username);
        	
        }
        out.println("<h1>Hello "+session.getAttribute("username")+"</h1>");
        HtmlInit.formInit(out , "/HelloWeb/Lotto2");
        HtmlInit.tableInit(out);
        HtmlInit.tableLineInit(out);
        for (int i = 0; i < 42; i++) 
        {
        	if (i%7 == 0)
        	{
        		HtmlInit.tableLineEnd(out);
        		HtmlInit.tableLineInit(out);
        	}
        	HtmlInit.tableCellCheckBox(out, "number", String.valueOf(i+1),String.valueOf(i+1) );
		}
        HtmlInit.tableLineEnd(out);
        HtmlInit.tableEnd(out);
        HtmlInit.input(out, "submit", "PLAY");
        HtmlInit.formEnd(out);
        HtmlInit.nextLine(out);
        
        if (request.getParameterValues("number")== null)
        {
        	out.print("Choose 6 Number");
        	HtmlInit.nextLine(out);
        	
        }
        else
        {
        	
        	choosedNumber = request.getParameterValues("number");
        	winningNumberArray = lotto.randomizeSelections();
    		lottoArray = lotto.toIntArray(winningNumberArray);
        	for (String s : choosedNumber)
   			{
   			choosedNumberArray.add(Integer.valueOf(s));
   			}
   			if (choosedNumberArray.size() == 6)
   			{	
   				session.setAttribute("compte", String.valueOf(compte));		
   				for (String s : choosedNumber) {
   					
   					choosedNumberArraySession.add(Integer.valueOf(s));
				}
   				for (Integer s : lottoArray) {
   					
   					winningNumberArraySession.add(s);
				}
   				session.setAttribute("winningNumberArraySession", winningNumberArraySession);
   				session.setAttribute("choosedNumberArraySession",choosedNumberArraySession );
   				out.print("<align = center>Choosed Numbers : ");
   				out.print(choosedNumberArray);
   				out.print("<p align=center>Winning Numbers : [");
   				
   				for (int i : lottoArray) 
   				{
   					c++;
   					if (c <lottoArray.length ) 
   					{
   						out.print(" "+i+", ");   		
   					} 
   					else 
   					{
   						out.print(" "+i);
   					}
   				}
   				out.println (" ]</p> <center>");
   				
   				for (int winningNumber : lottoArray) 
   				{
   					for (Integer playedNum : choosedNumberArray) 
   					{
   						if (winningNumber == playedNum) 
   						{
   							cpt++;
   						}
   					}
   				}
   				if (cpt == 6) 
   				{
   					out.print("YOU WIN");
   				} 
   				else 
   				{
   					out.print("YOU LOST");
   				}
   				HtmlInit.nextLine(out);	
   				HtmlInit.nextLine(out);
   				out.print("You have : "+cpt+" Correct Number ");
   				compte++;
   				HtmlInit.nextLine(out);
   				
   				
   				for (int i = 6; i < choosedNumberArraySession.size(); i++) {
					
   					if (i%6 == 0)
					{
   						out.print("ChoosedNumber on : "+(cache.size()+1)+" play");
						out.print(choosedNumberArraySession.subList(i-6,i));
						cache.put(String.valueOf(choosedNumberArraySession.subList(i-6, i)), j);
						HtmlInit.nextLine(out);
						
					}
				}
   				
   				for (int i = 6; i < winningNumberArraySession.size(); i++) {
					if (i%6 == 0)
					{
						out.print("WinningNumber on : "+(cache.size()-k)+" play");
						out.print(winningNumberArraySession.subList(i-6,i));
						cache.put(String.valueOf(winningNumberArraySession.subList(i-6, i)), j);
						HtmlInit.nextLine(out);
						
					}
					
				}
   				k++;
   				j++;
   			}
   			else
   			{
   				out.print("Choose 6 Number");
   			}
   			HtmlInit.nextLine(out);
   			out.print("You played "+compte+" times");
   			HtmlInit.nextLine(out);
   			out.print("Last time played : "+timeFormat.format(session.getLastAccessedTime()));
   			out.println("</center>");
   			
   			
        }
        
		HtmlInit.endInit(out);
	}
}
